package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.mapper.ResidentMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.dto.StudentCountDTO;
import com.example.mybatisplus.model.dto.UnderDTO;
import com.example.mybatisplus.service.AreaService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.ResidentService;
import com.example.mybatisplus.model.domain.Resident;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/resident")
public class ResidentController {

    private final Logger logger = LoggerFactory.getLogger(ResidentController.class);
    @Autowired
    private ResidentMapper residentMapper;
    @Autowired
    private ResidentService residentService;

    private AreaService areaService;

    @Autowired
    public ResidentController(AreaService areaService) {
        this.areaService = areaService;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Integer id) throws Exception {
        Resident resident = residentService.getById(id);
        return JsonResponse.success(resident);
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        residentService.removeById(id);
        return JsonResponse.success(null);
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateResident(Resident resident) throws Exception {
        residentService.updateById(resident);
        return JsonResponse.success(null);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Resident resident) throws Exception {
        residentService.save(resident);
        return JsonResponse.success(null);
    }
//园区统计
    @GetMapping("/count-by-campus")
    public ResponseEntity<Integer> countStudentsByCampusId(@RequestParam Long campusId) {
        int count = residentService.countStudentsByCampusId(campusId);
        return ResponseEntity.ok(count);
    }

    @GetMapping("/undergraduate_count")
    public ResponseEntity<Map<String, Integer>> getUndergraduateCount(
            @RequestParam String areaname,
            @RequestParam String campusname) {
        int count = residentMapper.getUndergraduateCount(areaname, campusname);
        Map<String, Integer> response = new HashMap<>();
        response.put("undergraduate_count", count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/master_count")
    public ResponseEntity<Map<String, Integer>> getMasterCount(
            @RequestParam String areaname,
            @RequestParam String campusname) {
        int count = residentMapper.getMasterCount(areaname, campusname);
        Map<String, Integer> response = new HashMap<>();
        response.put("master_count", count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/doctor_count")
    public ResponseEntity<Map<String, Integer>> getDoctorCount(
            @RequestParam String areaname,
            @RequestParam String campusname) {
        int count = residentMapper.getDoctorCount(areaname, campusname);
        Map<String, Integer> response = new HashMap<>();
        response.put("doctor_count", count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/teacher_count")
    public ResponseEntity<Map<String, Integer>> getTeacherCount(
            @RequestParam String areaname,
            @RequestParam String campusname) {
        int count = residentMapper.getTeacherCount(areaname, campusname);
        Map<String, Integer> response = new HashMap<>();
        response.put("teacher_count", count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/residentCountByMajor")
    public JsonResponse<List<StudentCountDTO>> getResidentCountByMajor(
            @RequestParam(name = "areaname") String areaName,
            @RequestParam(name = "campusname") String campusName) {
        List<StudentCountDTO> counts = residentService.getStudentCountByMajor(campusName, areaName);
        return JsonResponse.success(counts);
    }
    //房源分配
    @GetMapping("/checked-in-undergraduates")
    public ResponseEntity<Map<String, Integer>> getCheckedInUndergraduatesCount() {
        int checkedInCount = residentService.getCheckedInUndergraduatesCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("checked-in-undergraduates", checkedInCount);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/undergraduates")
    public ResponseEntity<Map<String, Integer>> getUndergraduatesCount() {
        int Count = residentService.getUndergraduatesCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("undergraduates", Count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/checked-in-masters")
    public ResponseEntity<Map<String, Integer>> getCheckedInMastersCount() {
        int checkedInCount = residentService.getCheckedInMastersCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("checked-in-masters", checkedInCount);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/masters")
    public ResponseEntity<Map<String, Integer>> getMastersCount() {
        int Count = residentService.getMastersCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("masters", Count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/checked-in-phds")
    public ResponseEntity<Map<String, Integer>> getCheckedInPhdsCount() {
        int checkedInCount = residentService.getCheckedInPhdsCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("checked-in-phds", checkedInCount);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/phds")
    public ResponseEntity<Map<String, Integer>> getPhdsCount() {
        int Count = residentService.getPhdsCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("phds", Count);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/checked-in-staffs")
    public ResponseEntity<Map<String, Integer>> getCheckedInStaffsCount() {
        int checkedInCount = residentService.getCheckedInStaffsCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("checked-in-staffs", checkedInCount);
        return ResponseEntity.ok(response);
    }
    @GetMapping("/staffs")
    public ResponseEntity<Map<String, Integer>> getStaffsCount() {
        int Count = residentService.getStaffsCount();
        Map<String, Integer> response = new HashMap<>();
        response.put("staffs", Count);
        return ResponseEntity.ok(response);
    }
    //
    @GetMapping("listUnder")
    public JsonResponse pageList1(UnderDTO underDTO, PageDTO dto) {
        Page<UnderDTO> page = residentService.getUnderBed(underDTO, dto);
        return JsonResponse.success(page);

    }
    @GetMapping("listPost")
    public JsonResponse pageList2(UnderDTO underDTO, PageDTO dto) {
        Page<UnderDTO> page = residentService.getPostBed(underDTO, dto);
        return JsonResponse.success(page);

    }
    @GetMapping("listStaff")
    public JsonResponse pageList3(UnderDTO underDTO, PageDTO dto) {
        Page<UnderDTO> page = residentService.getStaffBed(underDTO, dto);
        return JsonResponse.success(page);

    }
}

